package org.pager.utils;

import java.util.List;

/**
 * 通用（泛型）分页工具类 - 封装分页信息
 *
 * @author zqx
 * @date 2024-06-11
 */
public class PagerUtil<T> {
    /**
     * 当前页
     */
    private long currentPage;

    /**
     * 每页显示记录数
     */
    private int pageCount;

    /**
     * 总记录数
     */
    private long totalRecordCount;

    /**
     * 总页数
     */
    private int totalPageCount;

    /**
     * 页面显示的数据
     */
    List<T> data;


    // setter/getter
    public long getCurrentPage() {
        return currentPage;
    }

    /**
     * 设置当前页 - 合法性处理
     *
     * @param currentPage 当前页
     */
    public void setCurrentPage(int currentPage) {
        if (currentPage <= 0) {
            this.currentPage = 1;
        } else if (currentPage > this.getTotalPageCount()) {
            this.currentPage = this.getTotalPageCount();
        } else {
            this.currentPage = currentPage;
        }
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public long getTotalRecordCount() {
        return totalRecordCount;
    }

    public void setTotalRecordCount(long totalRecordCount) {
        this.totalRecordCount = totalRecordCount;
    }

    /**
     * 返回总页数
     *
     * @return 总页数
     */
    public long getTotalPageCount() {
        // 判断总记录数 和 每页记录数是否能整除
        if (this.getTotalRecordCount() % this.getPageCount() == 0) {
            return this.getTotalRecordCount() / this.getPageCount();
        }
        return this.getTotalRecordCount() / this.getPageCount() + 1;
    }

    // public void setTotalPageCount(int totalPageCount) {
    //    this.totalPageCount = totalPageCount;
    // }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
